---
title: 'Replication Döring 2016'
author: 'Holger Döring'
date: "`r format(Sys.time(), '%d %B %Y, %H:%M')`"
output:
  html_document:
    code_folding: hide
    toc: true
---

```{r}
dir.create(file.path('output'), showWarnings = FALSE)
print("create 'output' folder if it doesn't exist")
```

All replication results provided in [_output_](output) folder.


## Reference

Döring, H., 2016. Mapping established democracies: Integrated data on parties,
elections and cabinets. _Electoral Studies_.
[doi:10.1016/j.electstud.2016.07.002](http://dx.doi.org/10.1016/j.electstud.2016.07.002)


## Hix/Marsh replication

run [`ep-second-order.R`](ep-second-order.R) to create [Figure 2](output/figure-2.png) and
[Table 2](output/table-2.html) --- see [logfile](output/ep-second-order.log)

Hix, S., Marsh, M., 2007. Punishment or protest? Understanding European Parliament elections. Journal of Politics 69, 495–510.

```{r, eval=FALSE}
# run R chunk manually -- check date in log file
source('ep-second-order.R', echo = TRUE)
```


## ParlGov figures and tables


```{r, message=FALSE, warning=FALSE}
rm(list = ls())

pkgs <- c('dplyr', 'ggplot2', 'tidyr')
reqs <- vapply(pkgs, suppressMessages(require), character.only = TRUE, FUN.VALUE = logical(1))

parlgov_db <- src_sqlite('parlgov-2015-stable.db')
get_parlgov_table <- function(.) tbl(parlgov_db, .) %>% as.data.frame

country <- get_parlgov_table('country')
party <- get_parlgov_table('view_party')
name_change <- get_parlgov_table('party_name_change')
elec <- get_parlgov_table('view_election')
cab <- get_parlgov_table('view_cabinet')

# keep only post-1945 data
elec <- elec %>% filter(election_date >= '1945-01-01')
cab <- cab %>% filter(start_date >= '1945-01-01')

party_select <- party[party$party_id %in% c(elec$party_id, cab$party_id), 'party_id']
party <- party %>% filter(party_id %in% party_select, family_name_short != 'spec')

print("Read and select data from ParlGov database")
```

### Abstract

Summary information in abstract

```{r, message=FALSE, warning=FALSE}
sprintf('Number of countries: %d', country %>% nrow)
sprintf('Number of parties: %d', party %>% nrow)

sprintf('Number of elections: %d', elec %>% distinct(election_id) %>% nrow)
elec %>% group_by(election_type) %>% summarise(n = n_distinct(election_id)) %>% as.data.frame

sprintf('Number of cabinets: %d', cab %>% distinct(cabinet_id) %>% nrow)
```


### Table 1

Table 1 -- Summary of country information in ParlGov 2015 version

```{r, message=FALSE, warning=FALSE}

party_info <- party %>% count(country_id) %>% select(country_id, parties = n)

elec_info <- elec %>%
  group_by(country_id) %>%
  summarise(
    first = election_date %>% min %>% substr(., 1, 4) %>% as.integer,
    elections = n_distinct(election_id),
    results = n()
  )

cab_info <- cab %>%
  filter(cabinet_party == 1) %>%
  group_by(country_id) %>%
  summarise(
    cabinets = n_distinct(cabinet_id),
    members = n()
  )

info <- country %>%
  select(country_id = id, country = name) %>%
  left_join(party_info) %>%
  left_join(elec_info) %>%
  left_join(cab_info) %>%
  select(-country_id) %>%
  arrange(country) %>% 
  select(country, first, parties, elections:members)

write.csv(info, 'output/table-1.csv', na = '', fileEncoding = 'utf-8', row.names = FALSE)

info
```


### Figure 1

Figure 1. -- Political positions by party family

```{r, message=FALSE, warning=FALSE}
info_id_all <- get_parlgov_table('info_id')
fam_more <- get_parlgov_table('party_family')

# clean up data -- main party families only and non-imputed party positions
set_na_imputed <- . %>% ifelse(nchar(as.character(.)) > 3, ., NA)
party <- party %>%
  filter(! family_name_short %in% c('code', 'none', 'spec')) %>%
  mutate_each(funs(set_na_imputed), left_right:eu_anti_pro) %>%
  mutate(family = family_name_short)

# generate factor for party family and order by left/right classification
info_id <- info_id_all %>%
  filter(table_variable == 'party_family') %>%
  select(order, short, name) %>%
  arrange(order)
party$family_name <- factor(party$family_name, levels = info_id$name)
party$family_name_short <- factor(party$family_name_short, levels = info_id$short)
family_color <- c('#FB9A99', '#33A02C', '#E31A1C', '#FF7F00', '#A6CEE3', '#B2DF8A',
                  '#1F78B4', '#FDBF6F')  # http://colorbrewer2.org/

# Graph -- Left/right against main dimensions

pl_data <- party %>%
  select(family_name, left_right:eu_anti_pro) %>%
  gather(dimension, position, -family_name, -left_right)

pl_data$dimension <- factor(pl_data$dimension,
  levels = c('state_market', 'liberty_authority', 'eu_anti_pro'),
  labels = c('state/market', 'liberty/authority', 'EU anti/pro'))

pl_space <- ggplot(pl_data, aes(x = left_right, y = position)) +
  geom_point(aes(colour = family_name), shape = 3, size = 0.875) +
  stat_smooth(method = 'lm', formula = y ~ x + I(x^2)) +
  labs(x = 'left/right', y = '', colour = 'party family') +
  facet_grid(. ~ dimension) + theme_bw()
pl_space <- pl_space + scale_colour_manual(values = family_color)

ggsave(plot = pl_space, file = 'output/figure-1.png',  width = 7, height = 5, dpi = 300)

print(pl_space)
```


## R session info

```{r}
sessionInfo()
```

